local ROT = require 'rot'

local state = ROT.Class:extend()

local Item = require 'game.proto.item'
local Scene = require 'game.scene'

function state:init(game)
    self.game = game
    self.mode = 'species'
    self.player={
        alias = 'Somebody',
        name = 'you',
        definite = 'you',
        indefinite = 'you',
        glyph = '@',
        color = { 200, 200, 200 },

        healthRegen = 0.1,
        staminaRegen = 0.1,


        stuff = {},
        weapon = Item.fists,
        unarmed = Item.fists,

        str = 12,
        dex = 12,
        int = 12,

        evade = 10,
        speed = 0,

        -- skill levels, 0-10

        sneaking = 0, dodging = 0, blocking = 0,
        bashing = 0, slashing = 0, launching = 0,
        charming = 0, hexing = 0, nuking = 0,

        -- internal stuff
        delay = 0,
        lastHealth = 0,
        lastStamina = 0,

        ability = { 'fireball', 'firebreath' },
    }
end

function state:update()
    local game = self.game
    local display = game.display

    display:clear()

    if self.mode == 'species' then

        display:clear()

        display:write('Choose your species.', 1, 1)

        display:write('a) Human     (+0 str, +0 dex, +0 int)', 1, 3)

        display:write('b) Gnome     (-2 str, +1 dex, +1 int)', 1, 4)
        display:write('c) Dwarf     (+1 str, -2 dex, +1 int)', 1, 5)
        display:write('d) Troll     (+1 str, +1 dex, -2 int)', 1, 6)
        display:write('e) Golem     (+2 str, -1 dex, -1 int)', 1, 7)
        display:write('f) Sprite    (-1 str, +2 dex, -1 int)', 1, 8)
        display:write('g) Demon     (-1 str, -1 dex, +2 int)', 1, 9)

   elseif self.mode == 'background' then
        display:write('Choose your background.', 1, 1)

        display:write('a) Wanderer     (+0 str, +0 dex, +0 int)', 1, 3)
        display:write('b) Brawler      (+2 str, -1 dex, -1 int)', 1, 4)
        display:write('c) Scholar      (-1 str, -1 dex, +2 int)', 1, 5)
        display:write('d) Hunter       (-1 str, +2 dex, -1 int)', 1, 6)
   end
end

function state:keypressed(key)
    local game = self.game
    local player = self.player
    local done = false

    if self.mode == 'species' then

        if key == 'a' then
            player.species = 'Human'
            done = true
        elseif key == 'b' then
            player.species = 'Gnome'
            player.str = player.str - 2
            player.dex = player.dex + 1
            player.int = player.int + 1
            done = true
        elseif key == 'c' then
            player.species = 'Dwarf'
            player.str = player.str + 1
            player.dex = player.dex - 2
            player.int = player.int + 1
            done = true
        elseif key == 'd' then
            player.species = 'Troll'
            player.str = player.str + 1
            player.dex = player.dex + 1
            player.int = player.int - 2

            player.weapon = Item.claws
            player.unarmed = Item.claws

            done = true
        elseif key == 'e' then
            player.species = 'Golem'
            player.str = player.str + 2
            player.dex = player.dex - 1
            player.int = player.int - 1

            player.weapon = Item['heavy fists']
            player.unarmed = Item['heavy fists']
            done = true
        elseif key == 'f' then
            player.species = 'Sprite'
            player.str = player.str - 1
            player.dex = player.dex + 2
            player.int = player.int - 1
            done = true
        elseif key == 'g' then
            player.species = 'Demon'
            player.str = player.str - 1
            player.dex = player.dex - 1
            player.int = player.int + 2
            done = true
        end

        if done then self.mode = 'background' end

    elseif self.mode == 'background' then

        if key == 'a' then
            player.class = 'Wanderer'
            Scene:equip(player, 'stick')
            done = true
        elseif key == 'b' then
            player.class = 'Brawler'
            player.str = player.str + 2
            player.dex = player.dex - 1
            player.int = player.int - 1
            -- Scene:equip(player, 'hammer')
            done = true
        elseif key == 'c' then
            player.class = 'Scholar'
            player.str = player.str - 1
            player.dex = player.dex - 1
            player.int = player.int + 2
            Scene:equip(player, 'stick')
            done = true
        elseif key == 'd' then
            player.class = 'Hunter'
            player.str = player.str - 1
            player.dex = player.dex + 2
            player.int = player.int - 1
            Scene:equip(player, 'knife')
            done = true
        end
    end

    if player.species and player.class then
        player.maxHealth = player.str * 10
        player.maxStamina = player.dex * 10
        player.health = player.maxHealth
        player.stamina = player.maxStamina
        for i = 1, 5 do Scene:equip(player, 'healing potion') end
        for i = 1, 5 do Scene:equip(player, 'ninja potion') end
        for i = 1, 5 do Scene:equip(player, 'mapping scroll') end
        for i = 1, 5 do Scene:equip(player, 'ignition scroll') end
        for i = 1, 5 do Scene:equip(player, 'fog scroll') end
        for i = 1, 5 do Scene:equip(player, 'confuse scroll') end
        game.player = player
        game.scene = require 'game.scene.town' (game)
        game:pushState('play')
        game.scene:passTime(10)
    end
end

return state
